﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Shop.cs" company="">
//   
// </copyright>
// <summary>
//   The shop.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

/* 
 * Author: Mikkel Thordal Andersen, Juan Mompean Esteban
 * Created: 17.11.2011
 */

namespace ITU_vs_Zombies.Scenery
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.IO;

    using ITU_vs_Zombies.Game;
    using ITU_vs_Zombies.Stuff;

    using Microsoft.Xna.Framework;

    /// <summary>
    /// The shop.
    /// </summary>
    public class Shop
    {
        #region Constants and Fields

        /// <summary>
        /// The path.
        /// </summary>
        public static readonly string Path = Directory.GetCurrentDirectory() + @"\Content\shopItems.txt";

        /// <summary>
        /// The shop dictionary.
        /// </summary>
        public readonly List<ShopItem> ShopList;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Shop"/> class. 
        /// Create a new shop
        /// </summary>
        public Shop()
        {
            this.ShopList = new List<ShopItem>();
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The load items.
        /// </summary>
        public void LoadItems()
        {
            if (!File.Exists(Path))
            {
                return;
            }

            var lines = File.ReadAllLines(Path);
            foreach (var line in lines)
            {
                try
                {
                    var splitLine = line.Split(':');
                    ExtraObject extraObject = null;
                    switch (splitLine[0])
                    {
                        case "Gun":
                            extraObject = this.ParseFirearm(splitLine);
                            break;
                        case "Knife":
                            extraObject = this.ParseKnife(splitLine);
                            break;
                        case "Life":
                            extraObject = this.ParseLife(splitLine);
                            break;
                        default:
                            continue;
                    }

                    this.ShopList.Add(
                        new ShopItem
                            {
                                Name = splitLine[0], 
                                Price = uint.Parse(splitLine[1]), 
                                Item = extraObject, 
                                Image = splitLine[2]
                            });
                }
                catch
                {
                    // Ignore lines with errors
                }
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The parse firearm.
        /// </summary>
        /// <param name="splitLine">
        /// The split line.
        /// </param>
        /// <returns>
        /// The parsed extraObject
        /// </returns>
        private ExtraObject ParseFirearm(IList<string> splitLine)
        {
            Contract.Requires(splitLine.Count > 9);
            var firearm = new Firearm(
                @"Stuff\" + splitLine[2], 
                uint.Parse(splitLine[3]), 
                Client.MainPlayer.NumberFloor, 
                Vector2.Zero, 
                uint.Parse(splitLine[4]), 
                new TimeSpan(0, 0, int.Parse(splitLine[5])), 
                splitLine[6], 
                uint.Parse(splitLine[7]), 
                int.Parse(splitLine[8]),
                splitLine[9]);

            firearm.LoadContent();

            return firearm;
        }

        /// <summary>
        /// The parse knife.
        /// </summary>
        /// <param name="splitLine">
        /// The split line.
        /// </param>
        /// <returns>
        /// The parsed Knife
        /// </returns>
        private ExtraObject ParseKnife(IList<string> splitLine)
        {
            Contract.Requires(splitLine.Count > 6);
            var knife = new Knife(
                @"Stuff\" + splitLine[2], 
                uint.Parse(splitLine[3]), 
                Client.MainPlayer.NumberFloor, 
                Vector2.Zero, 
                uint.Parse(splitLine[4]), 
                uint.Parse(splitLine[5]),
                splitLine[6]);

            knife.LoadContent();

            return knife;
        }

        /// <summary>
        /// The parse life.
        /// </summary>
        /// <param name="splitLine">
        /// The split line.
        /// </param>
        /// <returns>
        /// The parsed Life.
        /// </returns>
        private ExtraObject ParseLife(IList<string> splitLine)
        {
            Contract.Requires(splitLine.Count > 5);
            var life = new Life(
                @"Stuff\" + splitLine[2], 
                uint.Parse(splitLine[3]), 
                Client.MainPlayer.NumberFloor, 
                Vector2.Zero, 
                uint.Parse(splitLine[4]),
                splitLine[5]);

            life.LoadContent();

            return life;
        }

        #endregion
    }
}